package com.example.coursetest1;

import jakarta.servlet.ServletException;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;

import java.io.IOException;
import java.io.PrintWriter;

@WebServlet(name = "LoginServlet", value = "/LoginServlet")
public class LoginServlet extends HttpServlet {

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String username = request.getParameter("username");
        String password = request.getParameter("password");

        String helloween = getServletConfig().getInitParameter("helloween");
        String admin = getServletConfig().getInitParameter("admin");
        String babyface = getServletConfig().getInitParameter("babyface");

        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();

        if (("helloween".equals(username) && helloween.equals(password)) ||
                ("admin".equals(username) && admin.equals(password)) ||
                ("babyface".equals(username) && babyface.equals(password))) {
            out.println("<html><body>");
            out.println("<h2>Welcome :</h2>");
            out.println("<p>Username: " + username + "</p>");
            out.println("</body></html>");
        } else if ("helloween".equals(username) || "admin".equals(username) || "babyface".equals(username)) {
            out.println("<html><body>");
            out.println("<h2>Password is incorrect!</h2>");
            out.println("</body></html>");
        } else {
            out.println("<html><body>");
            out.println("<h2>Username is not available!</h2>");
            out.println("</body></html>");
        }
    }
}